/**
 * $Id$
 * Copyright 2009-2012 Oak Pacific Interactive. All rights reserved.
 */
package com.rabbit.base;

import java.util.LinkedHashMap;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import com.rabbit.R;

/**
 *
 *
 * @author <a href="mailto:邮箱地址">姓名</a>
 * @version 2013-3-25上午11:11:49
 */
public class TagsFragment extends BaseFragment {

	public final String TAG = "com.quad.base.TabFragment";

	private Map<String, TabFragment> mFragments;

	private ViewGroup tabBarViewGroup;

//	public CommonTitleView mCommonTitleView;

	private String currentTag;

	private OnTagSelectListener mOnTagSelectListener;
	
	private OnTagLongClickListener mOnTagLongClickListener;

	private View rootView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.tab_fragment, container, false);
		tabBarViewGroup = (ViewGroup) rootView.findViewById(R.id.tab_bar);
		return rootView;
	}

	/**
	 * @author yuehui 创建的日期 ：2013-3-25
	 * @param fragment
	 * @param tag
	 *            fragment的标示 作用说明 ：添加fragment
	 */
	protected void addTabFragment(TabFragment fragment, Context context,
			String tag,LayoutInflater inflater) {
		if (mFragments == null) {
			mFragments = new LinkedHashMap<String, TabFragment>();
		}
		mFragments.put(tag, fragment);
		View tabBarView = fragment.getTableView(context, inflater,
				tabBarViewGroup);
		if (tabBarView == null) {
			tabBarView = createDefBarView(context, inflater, tabBarViewGroup,
					tag);
		}
		fragment.setTabsFragment(this);
		tabBarView.setTag(tag);
		tabBarViewGroup.addView(tabBarView);
		if (mOnTagSelectListener == null) {
			mOnTagSelectListener = new OnTagSelectListener();
		}
		if (mOnTagLongClickListener == null) {
		    mOnTagLongClickListener = new OnTagLongClickListener();
        }
		tabBarView.setOnClickListener(mOnTagSelectListener);
		tabBarView.setOnLongClickListener(mOnTagLongClickListener);
	}

	/**
	 * @author yuehui 创建的日期 ：2013-4-7
	 * @return 作用说明 ：获取当前的tag名字
	 */
	public String getCurrentTag() {
		return currentTag;
	}

	private View createDefBarView(Context context, LayoutInflater inflater,
			View rootView, String tag) {
		TextView t = new TextView(context);
		t.setText(tag);
		return t;
	}

	public TabFragment getTabFragmentByTag(String tag){
	    return mFragments == null ? null : mFragments.get(tag);
	}

	/**
	 * @author yuehui 创建的日期 ：2013-3-25
	 * @param fragmentTag
	 * @param toTag
	 *            作用说明 ：
	 */
	public void selectTabFragment(String tag) {
		if (TextUtils.isEmpty(tag)) {
			throw new NullPointerException();
		}
		View barView = tabBarViewGroup.findViewWithTag(tag);
		selectTabFragment(tag, barView);
	}

	private void selectTabFragment(String tag, View barView) {
		TabFragment fragment = mFragments.get(tag);
		if (fragment == null) {
			throw new IllegalAccessError("has no tag in tab group!");
		}
		treateOldTabState();
		FragmentTransaction transaction = getFragmentManager()
				.beginTransaction();
//		Log.v("yh", "------start------");
		if(!TextUtils.isEmpty(currentTag)) {
//		    Log.v("yh", "hide + currentTag = "+currentTag);
		    transaction.hide(mFragments.get(currentTag));
		}
		if(!fragment.isAdded) {
//		    Log.v("yh", "!--isAdd");
		    transaction.add(R.id.tab_fragment, fragment);
		    fragment.isAdded = true;
		}else {
//		    Log.v("yh", "show");
		    transaction.show(fragment);
		}
		if (rootView != null && rootView.getVisibility() != View.VISIBLE) {
            rootView.setVisibility(View.VISIBLE);
        }
		int s = transaction.commitAllowingStateLoss();
//		Log.v("yh", "commit over return = "+s);
//		Log.v("yh", "------over------");
		currentTag = tag;
		fragment.onSelect(barView);
	}
	
	   protected void initPreSelectTabFragment(String tag) {
	        TabFragment fragment = mFragments.get(tag);
	        if (fragment == null) {
	            throw new IllegalAccessError("has no tag in tab group!");
	        }
	        treateOldTabState();
	        FragmentTransaction transaction = getFragmentManager()
	                .beginTransaction();
	        if(!fragment.isAdded) {
	            transaction.add(R.id.tab_fragment, fragment);
	            fragment.isAdded = true;
	        }
	        
	        if(!TextUtils.isEmpty(currentTag)) {
	            transaction.hide(mFragments.get(currentTag));
	        }
	        
	        if (rootView != null && rootView.getVisibility() != View.GONE) {
	            rootView.setVisibility(View.GONE);
	        }
	        int s = transaction.commitAllowingStateLoss();
	        currentTag = tag;
	    }

	private void treateOldTabState() {
		TabFragment oldFragment = mFragments.get(currentTag);
		if (oldFragment == null) {
			return;
		}
		View barView = tabBarViewGroup.findViewWithTag(currentTag);
		oldFragment.onUnSelect(barView);
	}

	private class OnTagSelectListener implements View.OnClickListener {

		public void onClick(View v) {
			String tag = (String) v.getTag();
			if (TextUtils.isEmpty(tag) || tag.equals(currentTag)) {
				return;
			}
			TagsFragment.this.selectTabFragment(tag);
		}

	}
	
	private class OnTagLongClickListener implements View.OnLongClickListener {

        @Override
        public boolean onLongClick(View v) {
            String tag = (String) v.getTag();
            if(TextUtils.isEmpty(tag)){
                return false;
            }
            return false;
        }
	    
	}

	@Override
	public void onLowMemory() {
		super.onLowMemory();
		clearOtherFragmentData();
	}

	private void clearOtherFragmentData() {
		// TODO Auto-generated method stub
	}

	void hiddenTabBar() {
		Animation hiddenAnimation = new TranslateAnimation(0, 0, 0,
		        tabBarViewGroup.getHeight());
		hiddenAnimation.setDuration(300);
		tabBarViewGroup.setAnimation(hiddenAnimation);
		hiddenAnimation.start();
		tabBarViewGroup.setVisibility(View.GONE);
	}

	void showTabBar() {
		Animation hiddenAnimation = new TranslateAnimation(0, 0,
		        tabBarViewGroup.getHeight(), 0);
		hiddenAnimation.setDuration(300);
		tabBarViewGroup.setAnimation(hiddenAnimation);
		hiddenAnimation.start();
		hiddenAnimation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
			    tabBarViewGroup.setVisibility(View.VISIBLE);
			}
		});
	}


}
